package com.example.jspwork.Servlet;

import com.alibaba.fastjson.JSONObject;
import com.example.jspwork.Dao.AddressQuery;
import com.example.jspwork.pojo.InformBean;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(name = "UpdateServlet",value = "/api/update")
public class UpdateServlet  extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter pw = resp.getWriter();

        try{
            updateInform( new InformBean(
                    Integer.parseInt(req.getParameter("id")),
                    req.getParameter("city"),
                    req.getParameter("weather"),
                    req.getParameter("wind_dir"),
                    req.getParameter("wind_class"),
                    Integer.parseInt(req.getParameter("temp"))
                    )
            );

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("city", req.getParameter("city"));
            jsonObject.put("weather", req.getParameter("weather"));
            jsonObject.put("wind_dir", req.getParameter("wind_dir"));
            jsonObject.put("wind_class", req.getParameter("wind_class"));
            jsonObject.put("temp", req.getParameter("temp"));

            pw.println(jsonObject.toJSONString());

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    private  void updateInform(InformBean update_info){
        try{
            AddressQuery.update(update_info);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
